<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use api\wxapp\model\Problem;
class ProblemController extends AdminBaseController
{
    public function index()
    {
        $list = Problem::all();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'answer'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            $res = Problem::insert($_POST);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
    public function edit()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'answer'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $res = Problem::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $info = Problem::get($this->param()['id']);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function delete()
    {
        $params = $this->param();
        $res = Problem::destroy($params['id']);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}
